package cn.icanci.loopstack.bic.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.EdgeStyleService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.EdgeStyleVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.EdgeStyleQueryForm;
import cn.icanci.loopstack.bic.web.mapper.EdgeStyleWebMapper;
import cn.icanci.loopstack.bic.web.model.EdgeStyle;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/13 22:23
 */
@RestController
@RequestMapping("/bicAdmin/edgeStyle")
public class EdgeStyleController {

    @Resource
    private EdgeStyleWebMapper edgeStyleWebMapper;
    @Resource
    private EdgeStyleService   edgeStyleService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody EdgeStyleQueryForm form) {
        return R.builderOk()
            .data("queryPage", edgeStyleService.queryPage(edgeStyleWebMapper.web2vo(form.getEdgeStyle()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize()))
            .build();
    }

    @PostMapping("save")
    public R save(@RequestBody EdgeStyle edgeStyle) {
        edgeStyleService.save(edgeStyleWebMapper.web2vo(edgeStyle));
        return R.builderOk().build();
    }

    @GetMapping("validateName/{systemUuid}/{name}")
    public R validateName(@PathVariable("systemUuid") String systemUuid, @PathVariable("name") String name) {
        EdgeStyleVO edgeStyle = edgeStyleService.queryBySystemUuidAndName(systemUuid, name);
        return R.builderOk().data("result", edgeStyle == null).build();
    }

    @GetMapping("validateKey/{systemUuid}/{key}")
    public R validateKey(@PathVariable("systemUuid") String systemUuid, @PathVariable("key") String key) {
        EdgeStyleVO edgeStyle = edgeStyleService.queryBySystemUuidAndKey(systemUuid, key);
        return R.builderOk().data("result", edgeStyle == null).build();
    }
    // ================================ Load Selectors ================================

    @GetMapping("loadSelector/{systemUuid}")
    public R loadSelector(@PathVariable("systemUuid") String systemUuid) {
        List<TextValue> textValues = edgeStyleService.loadSelector(systemUuid);
        return R.builderOk().data("textValues", textValues).build();
    }

    @GetMapping("loadDefaultStyle")
    public R loadDefaultStyle() {
        String defaultStyle = edgeStyleService.loadDefaultStyle();
        return R.builderOk().data("defaultStyle", defaultStyle).build();
    }

}
